package com.gitee.fsmxm.ocon.converter;

import com.gitee.fsmxm.ocon.core.Converter;
import com.gitee.fsmxm.ocon.model.ConParam;
import com.gitee.fsmxm.ocon.model.Procedure;
import com.gitee.fsmxm.ocon.util.PptUtils;
import com.jacob.com.Dispatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * word open document and convert to ..
 *
 * @author fsmxm
 */
public class PowerpointConverter implements Converter {

    private static final Logger LOGGER = LoggerFactory.getLogger(PowerpointConverter.class);

    @Override
    public void convert(Procedure procedure, ConParam param) {
        Dispatch document = null;
        try {
            document = Dispatch.invoke(
                    procedure.getDispatch(),
                    "Open",
                    Dispatch.Method,
                    PptUtils.paramOfOpen(param.getSource().getFile().getAbsolutePath(), true, false, false),
                    new int[1]).toDispatch();
            Dispatch.call(
                    document,
                    "SaveAs",
                    param.getTarget().getFile().getAbsolutePath(),
                    PptUtils.getFormatType(param.getTarget().getExt()));
        } finally {
            if (document != null) {
                try {
                    Dispatch.call(document, "Close");
                    document.safeRelease();
                } catch (Exception e) {
                    LOGGER.warn("exception: ", e);
                }
            }
        }
    }

}
